﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web;

using VASJ.Model;


namespace VASJ.Service.Library.Manager
{
    public class ProjectTypeManager : BaseManager
    {
        /// <summary>
        /// Init
        /// </summary>
        /// <param name="_ApplicationName"></param>
        public ProjectTypeManager(string _ApplicationName)
            : base(_ApplicationName)
        {

        }
        /// <summary>
        /// Author: Hoang Van Tai
        /// </summary>
        /// <returns></returns>
        public List<ProjectType> GetProjectTypeListBySA(bool IsPM, ref string msgError)
        {
            try
            {
                List<ProjectType> ret = new List<ProjectType>();
                var query = from p in this.mdb.AHSProjectTypes
                            where (p.IsPM.Value == IsPM) && 
                                (p.ApplicationName.ToLower() == this.ApplicationName.ToLower())
                            orderby p.Id ascending
                            select p;

                foreach (var item in query)
                {
                    ret.Add(new ProjectType(item.Id, item.Name, item.PathIcon, item.IsPM, item.ApplicationName));
                }

                return ret;
            }
            catch (System.Exception ex)
            {
                Logger.Log.ErrorFormat("[" + this.ApplicationName + "] ProjectTypeManager.GetProjectTypeListBySA {0}", ex);
                msgError = ex.Message;

                return null ;
            }
        }

        /// <summary>
        /// Author:Le Ngoc Thach
        /// </summary>
        /// <returns></returns>
        public List<ProjectType> GetProjectTypeList()
        {
            try
            {
                UserManager um = new UserManager(this.ApplicationName);
                List<ProjectType> ret = new List<ProjectType>();
                
                var list = (from m in this.mdb.AHSProjectTypes
                           where (m.ApplicationName.ToLower() == this.ApplicationName.ToLower())
                           orderby m.Id ascending
                           select m).Distinct();

                foreach (var item in list)
                {
                    ret.Add(new ProjectType(item.Id, item.Name, item.PathIcon, item.IsPM, item.ApplicationName));
                }

                return ret;
            }
            catch (System.Exception ex)
            {
                Logger.Log.ErrorFormat("[" + this.ApplicationName + "] ProjectTypeManager.GetProjectTypeList {0}", ex); 
                return null;
            }
        }
    }
}